﻿using System.Diagnostics;
using System;
using FStore.Utils;

namespace FStore.Errors
{
    /// <summary>
    /// Provides methods for dealing with errors
    /// </summary>
    public static class ErrorHelper
    {
        /// <summary>
        /// Throws an ArgumentException should a condition for a parameter not be met
        /// </summary>
        /// <typeparam name="T">The type of the parameter</typeparam>
        /// <param name="name">The parameter name</param>
        /// <param name="value">The parameter value</param>
        /// <param name="condition">The condition that should hold true, a <c>false</c> value results in the exception</param>
        /// <param name="message">The message to use with the exception</param>
        [DebuggerHidden]
        public static void GenInvaildParam<T>(string name, T value, bool condition, string message = null)
        {
            // If the condition is false
            if (!condition)
            {
                // Get the stack trace
                StackTrace st = new StackTrace();

                // Generate the default message
                string methodName = st.GetFrame(1).GetMethod().Name;
                string msg = "An invalid parameter value({0}) for [{1}] was supplied to [{2}]".UFormat(
                    value != null ? value.ToString() : "NULL", name, methodName);

                // If a message was supplied, overwrite the existing
                if (string.IsNullOrEmpty(message) == false)
                    msg = message;

                // Throw the exception
                throw new ArgumentException(msg);
            }
        }

        [DebuggerHidden]
        public static void GenError(bool condition, string message = null)
        {
            // If the condition is false
            if (!condition)
            {
                // Get the stack trace
                StackTrace st = new StackTrace();

                // Generate the default message
                string methodName = st.GetFrame(1).GetMethod().Name;
                string msg = "An error occurred in {0}".UFormat(methodName);

                // If a message was supplied, overwrite the existing
                if (string.IsNullOrEmpty(message) == false)
                    msg = message;

                // Throw the exception
                throw new Exception(msg);
            }
        }
    }
}